#[path= "../util/result.rs"]
mod result;
#[path= "../entity/user.rs"]
mod user;
use actix_web::{delete, get, post, put, web, HttpResponse};
use sqlx::MySqlPool;
use user::UserEntity;
use result::Rest;
pub struct UserService;

#[get("")]
// sql执行过程中遇到null,rust后端没有进行处理
pub async fn get_users(pool: web::Data<MySqlPool>) -> HttpResponse {
    match sqlx::query_as::<_, UserEntity>("SELECT id, name, gender, birth_date, entry_date, employment_status, unit_name, unit_code, education, highest_degree FROM art_college_teachers")
        .fetch_all(&**pool)
        .await
    {
        Ok(users) => HttpResponse::Ok().json(Rest::ok(users)),
        Err(e) => HttpResponse::InternalServerError().json(Rest::<()>::err(format!("Database error: {}", e))),
    }
}

#[get("/{id}")]
pub async fn get_user_by_id(
    pool: web::Data<MySqlPool>,
    id: web::Path<i32>,
) -> HttpResponse {
    match sqlx::query_as::<_, UserEntity>("SELECT id, name, gender, birth_date, entry_date, employment_status, unit_name, unit_code, education, highest_degree FROM art_college_teachers WHERE id = ?")
        .bind(id.into_inner())
        .fetch_one(&**pool)
        .await
    {
        Ok(user) => HttpResponse::Ok().json(Rest::ok(user)),
        Err(e) => HttpResponse::InternalServerError().json(Rest::<()>::err(format!("Database error: {}", e))),
    }
}

#[post("")]
pub async fn create_user(
    pool: web::Data<MySqlPool>,
    new_user: web::Json<UserEntity>,
) -> HttpResponse {
    let user = new_user.into_inner();
    match sqlx::query("INSERT INTO users (name, gender, birth_date, entry_date, employment_status, unit_name, unit_code, education, highest_degree) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)")
        .bind(&user.name)
        .bind(&user.gender)
        .bind(&user.birth_date)
        .bind(&user.entry_date)
        .bind(&user.employment_status)
        .bind(&user.unit_name)
        .bind(&user.unit_code)
        .bind(&user.education)
        .bind(&user.highest_degree)
        .execute(&**pool)
        .await
    {
        Ok(_) => HttpResponse::Created().json(Rest::ok(user)),
        Err(e) => HttpResponse::InternalServerError().json(Rest::<()>::err(format!("Database error: {}", e))),
    }
}

#[put("/{id}")]
pub async fn update_user(
    pool: web::Data<MySqlPool>,
    id: web::Path<i32>,
    updated_user: web::Json<UserEntity>,
) -> HttpResponse {
    let user = updated_user.into_inner();
    match sqlx::query("UPDATE users SET name = ?, gender = ?, birth_date = ?, entry_date = ?, employment_status = ?, unit_name = ?, unit_code = ?, education = ?, highest_degree = ? WHERE id = ?")
        .bind(&user.name)
        .bind(&user.gender)
        .bind(&user.birth_date)
        .bind(&user.entry_date)
        .bind(&user.employment_status)
        .bind(&user.unit_name)
        .bind(&user.unit_code)
        .bind(&user.education)
        .bind(&user.highest_degree)
        .bind(id.into_inner())
        .execute(&**pool)
        .await
    {
        Ok(_) => HttpResponse::Ok().json(Rest::ok(user)),
        Err(e) => HttpResponse::InternalServerError().json(Rest::<()>::err(format!("Database error: {}", e))),
    }
}

#[delete("/{id}")]
pub async fn delete_user(
    pool: web::Data<MySqlPool>,
    id: web::Path<i32>,
) -> HttpResponse {
    match sqlx::query("DELETE FROM users WHERE id = ?")
        .bind(id.into_inner())
        .execute(&**pool)
        .await
    {
        Ok(_) => HttpResponse::NoContent().finish(),
        Err(e) => HttpResponse::InternalServerError().json(Rest::<()>::err(format!("Database error: {}", e))),
    }
}